package com.xinjie.www.servlet;

import com.xinjie.www.po.Ban;
import com.xinjie.www.po.Tieba;
import com.xinjie.www.po.User;
import com.xinjie.www.service.ban.BanServiceImpl;
import com.xinjie.www.service.comment.CommentServiceImpl;
import com.xinjie.www.service.tieba.TiebaServiceImpl;
import com.xinjie.www.util.Constants;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class CommentServlet extends HttpServlet {
    CommentServiceImpl commentService = new CommentServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if ("write".equals(method)) {
            this.writeComment(req, resp);
        }
        if ("delete".equals(method)) {
            try {
                this.deleteComment(req,resp);
            } catch (SQLException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    private void writeComment(HttpServletRequest req, HttpServletResponse resp) {
        int postId = Integer.parseInt(req.getParameter("postId"));
        String content = req.getParameter("content");
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String time = sdf.format(date);
        Object o = req.getSession().getAttribute(Constants.USER_SESSION);
        User user = (User) o;
        try {
            boolean b = false;
            if (!"".equals(content)) {
                b = commentService.writeComment(postId, user.getUserId(), content, time);
            }
            if (b) {
                req.getSession().setAttribute("commentSuccess", "发布评论成功");
                resp.sendRedirect("/UserPage/PostList.jsp");
            } else {
                req.getSession().setAttribute("commentLose", "发布评论失败");
                resp.sendRedirect("/UserPage/PostList.jsp");
            }
        } catch (SQLException | ClassNotFoundException | IOException e) {
            e.printStackTrace();
        }
    }

    private void deleteComment(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ClassNotFoundException, IOException {
        int commentId = Integer.parseInt(req.getParameter("commentId"));
        int postId = Integer.parseInt(req.getParameter("postId"));
        boolean b = commentService.deleteOwnComment(commentId, postId);
        String identity = req.getParameter("identity");
        if (b){
            req.getSession().setAttribute("deleteSuccess","删除成功");
            if ("own".equals(identity)){
                resp.sendRedirect("/UserPage/OwnComment.jsp");
            }else {
                resp.sendRedirect("/UserPage/LookOwnPost.jsp");
            }
        }else {
            req.getSession().setAttribute("deleteLose","删除失败");
            if ("own".equals(identity)){
                resp.sendRedirect("/UserPage/OwnComment.jsp");
            }else {
                resp.sendRedirect("/UserPage/LookOwnPost.jsp");
            }
        }
    }
}
